package com.eastmelon.blog.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.basic.model.SystemContext;
import com.eastmelon.blog.entity.Article;
import com.eastmelon.blog.service.ArticleService;
import com.eastmelon.blog.service.ChannelService;
import com.eastmelon.blog.service.LinkService;
import com.eastmelon.blog.service.TagsService;
import com.eastmelon.blog.service.UserService;
import com.eastmelon.blog.util.LuceneUtil;

@Controller("indexController")
public class IndexController {
	@Resource(name = "articleService")
	private ArticleService articleService;
	@Resource(name = "channelService")
	private ChannelService channelService;
	@Resource(name = "linkService")
	private LinkService linkService;
	@Resource(name = "tagsService")
	private TagsService tagsService;
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping({ "/index", "/" })
	public String index() {
		return "index";
	}

	@RequestMapping("/article/{id}")
	public String articleShow(@PathVariable int id, Model model) {
		Article article = articleService.get(id);
		model.addAttribute("a", article);
		model.addAttribute("pre", articleService.getPreItem(id));
		model.addAttribute("next", articleService.getNextItem(id));
		return "index/article/articleShow";
	}

	@RequestMapping("/category/{id}")
	public String listArticleByCategory(@PathVariable int id, Model model) {
		SystemContext.setPageSize(5);
		Pager<Article> pa = articleService.findByCategory(id);
		String channelName=channelService.getChannelName(id);
		model.addAttribute("channelName", channelName);
		model.addAttribute("id", id);
		model.addAttribute("pa", pa);
		return "index/article/articleList";
	}

	@RequestMapping("/dateCategory/{dateCategory}")
	public String listArticleByCategory(@PathVariable String dateCategory, Model model) {
		SystemContext.setPageSize(5);
		Pager<Article> pa = articleService.findByDateCategory(dateCategory);
		String channelName=dateCategory;
		model.addAttribute("channelName", channelName);
		model.addAttribute("dateCategory", dateCategory);
		model.addAttribute("pa", pa);
		return "index/article/articleList";
	}

	@RequestMapping("/tags/{id}")
	public String listArticleByTags(@PathVariable int id, Model model) {
		SystemContext.setPageSize(5);
		Pager<Article> pa = articleService.findByTags(id);
		String channelName=tagsService.getTagName(id);
		model.addAttribute("channelName", channelName);
		model.addAttribute("id", id);
		model.addAttribute("pa", pa);
		return "index/article/articleList";
	}

	@RequestMapping("/article")
	public String listArticle(Model model) {
		SystemContext.setPageSize(5);
		Pager<Article> pa = articleService.findPage();
		model.addAttribute("pa", pa);
		return "index/article/articleList";
	}

	@RequestMapping("/search/{param}")
	public String listArticle(@PathVariable String param, Model model)
			throws IOException, ParseException, InvalidTokenOffsetsException {
		List<Article> la = new ArrayList<Article>();
		if (param != null && !"".equals(param)) {
			la = LuceneUtil.Searcher(param);
		}
		param=HtmlUtils.htmlEscape(param);
		System.out.println(param);
		model.addAttribute("word", param);
		model.addAttribute("la", la);
		return "index/article/searchResult";
	}

}